package com.ztgf.pay.api.client;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.pay.models.bo.PayBatchByRemarksBO;
import com.ztgf.pay.models.bo.PayOrderMotherSaveBO;
import com.ztgf.pay.models.dto.PayOrderMotherDTO;
import com.ztgf.pay.models.service.IPayOrderMotherService;
import com.ztgf.pay.models.vo.PayBatchPrintVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.ztgf.pay.models.service.IPayOrderService;
import com.ztgf.pay.models.bo.PayOrderBO;
import com.ztgf.pay.models.dto.PayOrderDTO;

import java.util.List;

/**
 * 支付订单控制器
 *
 * @author mxli
 * @Date 2020-07-30 12:12:21
 */

@RestController
@RequestMapping("/api/payOrder")
@Slf4j
@Api(value = "PayOrderApiController",tags = "支付订单Api接口")
public class PayOrderApiController {

    @DubboReference
    private IPayOrderService payOrderService;

    @DubboReference
    private IPayOrderMotherService payOrderMotherService;


    @PostMapping("/saveOnlineBankPay")
    @ApiOperation(value = "网银支付",notes = "网银支付")
    public Result<PayOrderMotherDTO> saveOnlineBankPay(PayOrderMotherSaveBO saveBO){
        return payOrderService.saveOnlineBankPay(saveBO);
    }

    @PostMapping("/saveBalancePay")
    @ApiOperation(value = "余额支付",notes = "余额支付")
    public Result saveBalancePay(PayOrderMotherSaveBO saveBO){
        return payOrderService.saveBalancePay(saveBO);
    }

    @PostMapping("/saveOnlineBankPayLog")
    @ApiOperation(value = "保存查询网银支付状态",notes = "保存查询网银支付状态")
    public void saveOnlineBankPayLog(){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("type", 1);
        Object[] status = {0, 1};
        wrapper.in("status", status);
        List<PayOrderMotherDTO> list = payOrderMotherService.selectList(wrapper);
        if(list != null && list.size() > 0){
            for (PayOrderMotherDTO dto : list) {
                payOrderService.saveOnlineBankPayLog(dto);
            }
        }
    }

    @PostMapping("/saveBalancePayLog")
    @ApiOperation(value = "保存查询余额支付状态",notes = "保存查询余额支付状态")
    public void saveBalancePayLog(){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("type", 0);
        Object[] status = {0, 1};
        wrapper.in("status", status);
        List<PayOrderDTO> list = payOrderService.selectList(wrapper);
        if(list != null && list.size() > 0){
            for (PayOrderDTO dto : list) {
                payOrderService.saveBalancePayLog(dto);
            }
        }
    }

    @PostMapping("/printingElectronicReceipt")
    @ApiOperation(value = "打印电子回单",notes = "打印电子回单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId",value = "店铺ID",required = true,paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "orderId",value = "订单ID",required = true,paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "date",value = "订单日期格式字符串(yyyy-MM-dd HH:mm:ss)",required = true,paramType = "query", dataType = "String")
    })
    public Result<List<String>> printingElectronicReceipt(String orderId,String shopId,String date){
        Result<List<String>> result;
        if (StrUtils.isEmpty(shopId) || StrUtils.isEmpty(date) || StrUtils.isEmpty(orderId)) {
            result =  new Result<>(CodeEnum.FAIL);
            return result;
        }
        List<String> electronicReceiptUrl = payOrderService.printingElectronicReceipt(orderId,shopId,date);

        if (electronicReceiptUrl.isEmpty()) {
            result =  new Result<>(CodeEnum.ELECTRONIC_RECEIPT_QUERY_FAILED);
        } else {
            result = new Result<>();
            result.setData(electronicReceiptUrl);
        }
        return result;
    }

    @PostMapping("/printingSingleElectronicReceiptByDetail")
    @ApiOperation(value = "根据备注单个打印回单",notes = "根据备注单个打印回单")
    public Result<PayBatchPrintVO> printingSingleElectronicReceiptByDetail(PayBatchByRemarksBO payBatchByRemarksBO){
        PayBatchPrintVO payBatchPrintVO = payOrderService.printingSingleElectronicReceiptByDetail(payBatchByRemarksBO);
        return new Result<>(payBatchPrintVO);
    }

    @PostMapping("/printingBatchElectronicReceiptByDetail")
    @ApiOperation(value = "根据备注批量打印回单",notes = "根据备注批量打印回单")
    public Result<PayBatchPrintVO> printingBatchElectronicReceiptByDetail(String list){
        List<PayBatchByRemarksBO> payBatchByRemarksBOS = JSONArray.parseArray(list, PayBatchByRemarksBO.class);
        PayBatchPrintVO payBatchPrintVO = payOrderService.printingBatchElectronicReceiptByDetail(payBatchByRemarksBOS);
        return new Result<>(payBatchPrintVO);
    }
}
